-- Nueva funcion para el modulo de libros fiscales y electronicos. Para retenciones por conceptos.


CREATE OR REPLACE VIEW vmovcomlb AS 
 SELECT comm.id AS idcomm, vm.tipomov, vm.idctb, vm.id, vm.piva, vm.orden, 
    comm.idpadfiscal AS idpad, comm.fecha, comm.fechaemision, comm.tipocom, 
    comm.letra, comm.nroptovta, comm.nro, comm.nrofinal, comm.nroserie, 
    comm.detalle, comm.codambsuc, comm.codpadiva, comm.razonsocialfiscal, 
    comm.tipocuitfiscal, comm.cuitfiscal, comm.codcai, comm.vtocai, 
    comm.tipocai, 
        CASE
            WHEN padrig_concepto.id IS NOT NULL THEN padrig_concepto.id
            ELSE comm.idpadrig
        END AS idpadrig, 
    cl.codctblib, vm.signo AS signopse, vm.impo AS impopse, 
    vm.impo * cl.signo::numeric AS impomov, vm.signo * cl.signo AS signomov, 
    vm.alicuota, conm.base, conm.id AS idconcep
   FROM ctblibcom cl
   JOIN comm ON cl.tipocom::text = comm.tipocom::text
   LEFT JOIN commov vm ON comm.id = vm.idcomm
   LEFT JOIN conm ON conm.id = vm.id AND vm.tipomov::text = 'CON'::text
   LEFT JOIN pad ON pad.id = comm.idpad
   LEFT JOIN (SELECT EsInscripto, IdConcep, max(Id) as Id FROM padrig 
              GROUP BY EsInscripto, IdConcep) padrig_concepto
              ON conm.idcon = padrig_concepto.idconcep AND padrig_concepto.esinscripto::text = pad.esinscganancias::text;

ALTER TABLE vmovcomlb
  OWNER TO abacouser;

-- =====================================
   SELECT aaversion('398.00');
-- =====================================

